var _token = $('meta[name="csrf-token"]').attr("content"); //获取csrf 因为ajax POST提交需要
var setting = {
    check: {
        enable: true
    },
    data: {
        key: {
            title: "title"
        },
        simpleData: {
            enable: true
        }
    },
    callback: {
        onCheck: onCheck
    },
    async: {
        enable: true,
        type: "post",
        url: "/groups/group/organization",
        autoParam: ["id", "name"],
        otherParam: {"otherParam": "zTreeAsyncTest", _csrf: _token},
        dataFilter: filter //异步返回后经过Filter 

    }
};

function filter(treeId, parentNode, childNodes) {
    if (!childNodes)
        return null;
   var  childNode1 = eval(childNodes.groups);
    for (var i = 0, l = childNode1.length; i < l; i++) {
        childNode1[i].name = childNode1[i].name.replace(/\.n/g, '.');
    }
    return childNode1;
}
var zNodes = [];    
var zNodes2222 = [
    {id: 1, pId: 0, name: "父节点1", title: "", checked: true, open: true},
    {id: 11, pId: 1, name: "父节点11", title: "", checked: true},
    {id: 111, pId: 11, name: "叶子节点111", title: "", checked: true, isHidden: true},
    {id: 112, pId: 11, name: "叶子节点112", title: ""},
    {id: 113, pId: 11, name: "叶子节点113", title: ""},
    {id: 12, pId: 1, name: "父节点12", title: "", isHidden: true},
    {id: 121, pId: 12, name: "叶子节点121", title: ""},
    {id: 122, pId: 12, name: "叶子节点122", title: "", isHidden: true},
    {id: 123, pId: 12, name: "叶子节点123", title: ""},
    {id: 2, pId: 0, name: "父节点2", title: ""},
    {id: 21, pId: 2, name: "父节点21", title: "", isHidden: true},
    {id: 211, pId: 21, name: "叶子节点211", title: ""},
    {id: 212, pId: 21, name: "叶子节点212", title: ""},
    {id: 213, pId: 21, name: "叶子节点213", title: ""},
    {id: 22, pId: 2, name: "父节点22", title: ""},
    {id: 221, pId: 22, name: "叶子节点221", title: ""},
    {id: 222, pId: 22, name: "叶子节点222", title: ""},
    {id: 223, pId: 22, name: "叶子节点223", title: ""}
];
function onCheck(e, treeId, treeNode) {
    count();
}

function setTitle(node) {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    var nodes = node ? [node] : zTree.transformToArray(zTree.getNodes());
    for (var i = 0, l = nodes.length; i < l; i++) {
        var n = nodes[i];
         
        n.title = "[" + n.id + "] isFirstNode = " + n.isFirstNode + ", isLastNode = " + n.isLastNode;
        zTree.updateNode(n);
    }
}
function count() {
    function isForceHidden(node) {
        if (!node.parentTId)
            return false;
        var p = node.getParentNode();
        return !!p.isHidden ? true : isForceHidden(p);
    }
    var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            checkCount = zTree.getCheckedNodes(true).length,
            nocheckCount = zTree.getCheckedNodes(false).length,
            hiddenNodes = zTree.getNodesByParam("isHidden", true),
            hiddenCount = hiddenNodes.length;
    for (var i = 0, j = hiddenNodes.length; i < j; i++) {
        var n = hiddenNodes[i];
        if (isForceHidden(n)) {
            hiddenCount -= 1;
        } else if (n.isParent) {
            hiddenCount += zTree.transformToArray(n.children).length;
        }
    }

    $("#isHiddenCount").text(hiddenNodes.length);
    $("#hiddenCount").text(hiddenCount);
    $("#checkCount").text(checkCount);
    $("#nocheckCount").text(nocheckCount);
}

function showNodes() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getNodesByParam("isHidden", true);
    zTree.showNodes(nodes);
    setTitle();
    count();
}
function hideNodes() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getSelectedNodes();
    if (nodes.length == 0) {
        alert("请至少选择一个节点");
        return;
    }
    zTree.hideNodes(nodes);
    setTitle();
    count();
}
//选择点击chebox框
function onCheck(e, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getCheckedNodes(true),
            v = "";
    for (var i = 0, l = nodes.length; i < l; i++) {
        v += nodes[i].name + "/";
    }
    if (v.length > 0)
        v = v.substring(0, v.length - 1);
    var cityObj = $("#cust-btn-ok");
    cityObj.attr("value", v);
    //发送给ifrom 确定按钮
    window.parent.document.getElementById('cust-btn-ok').setAttribute('attrs', cityObj.val());
}

//初始化数据
$(document).ready(function() {
    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    $("#hideNodesBtn").bind("click", {type: "rename"}, hideNodes);
    $("#showNodesBtn").bind("click", {type: "icon"}, showNodes);
    setTitle();
    count();
});
